package main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

import queries.MapQuery;
import queries.mapAssign;
import queries.marginalQuery;
import graph.Graph;
import jTree.JunctionTree;
import jTree.Node;
import jTree.NodeSet;

public class MainProg {

	public static void main(String args[]){
		
		if(args.length != 2){
			System.out.println("Usage: main.MainProg <full path to structure file> <full path to potential file>");
		}	
		
		Graph g = new Graph(args[0],args[1]);		// creating the initial graph object
		//g.readGraph();				// Reading data and filling the initial structure
		//g.traingulate();			    // Triangulating the graph
		
		JunctionTree tree = new JunctionTree(g);	//creating junction tree from the triangulated graph
		
		MapQuery MAP = new MapQuery(tree);
		MAP.getMap(MAP.largestCliqueIndex, null, null, null);
		
		ArrayList<Integer> q = new ArrayList<Integer>();
		
		System.out.println("Enter the Nodes (space separated) whose marginal is to be found");
		Scanner s = new Scanner(System.in);
		
		String num[] = s.nextLine().split(" ");
		
		for(int i = 0; i<num.length; i++)
			q.add(Integer.parseInt(num[i]));
		
		s.close();
				
		marginalQuery mq = new marginalQuery(q,tree);
		
		mq.getMarginal(mq.root,null,null);
	
		System.out.println("Marginal obtained");
		
		for(List<Integer> k : mq.marginalTable.keySet() ){
			
			for(int val: k)
				System.out.print(val+" ");
		
			System.out.println(mq.marginalTable.get(k));
		}
	
		//System.out.println("Checking clique potentials after marginalization");
		
		/*for(NodeSet clq : tree.treeNodes){
			System.out.println("Clique "+tree.treeNodes.indexOf(clq));
			for(int i = 0; i<clq.table.length; i++){
				System.out.println(i+" "+clq.table[i].value);
			}
		}*/
		
		
		
		
		
		
		/*System.out.println("For the second time");
	
		mq = new marginalQuery(q,tree);
		mq.getMarginal(mq.root,null,null);
	
		System.out.println("Marginal obtained");
		
		for(List<Integer> k : mq.marginalTable.keySet() ){
			
			for(int val: k)
				System.out.print(val+" ");
		
			System.out.println(mq.marginalTable.get(k));
		}*/
	
	
	
	}
}
